package com.alogic.commonmark.reattr;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Plugins
 * @since 1.6.13.11 [20200901 duanyy]
 */
public class Plugins extends Segment {

    public Plugins(String tag, Logiclet p) {
        super(tag, p);

        registerModule("md-set-attr",SetAttr.class);
        registerModule("md-get-attr",GetAttr.class);
    }

    /**
     * 获取属性
     */
    public static class GetAttr extends AbstractLogiclet{
        protected String pid = "$md-reattr";
        protected String $id;
        protected String $key;
        protected String $dft;
        protected boolean raw = false;

        public GetAttr(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            pid = PropertiesConstants.getString(p,"pid",pid);
            $id = PropertiesConstants.getRaw(p,"id","");
            $key = PropertiesConstants.getRaw(p,"key","");
            $dft = PropertiesConstants.getRaw(p,"dft","");
        }

        @Override
        protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
            Map<String,String> attrMap = ctx.getObject(pid);
            if (attrMap != null){
                String id = PropertiesConstants.transform(ctx,$id,"");
                String value = null;
                if (StringUtils.isNotEmpty(id)){
                    if (StringUtils.isEmpty($key)){
                        value = attrMap.get(id);
                    }else{
                        String key = PropertiesConstants.transform(ctx,$key,"");
                        if (StringUtils.isEmpty(key)){
                            value = attrMap.get(id);
                        }else{
                            value = attrMap.get(key);
                        }
                    }
                    if (StringUtils.isEmpty(value)){
                        value = PropertiesConstants.transform(ctx,$dft,"");
                    }
                    if (StringUtils.isNotEmpty(value)){
                        ctx.SetValue(id,value);
                    }
                }
            }else{
                log(String.format("[%s]parent context id null:%s",this.getXmlTag(),pid));
            }
        }
    }

    /**
     * 设置属性
     */
    public static class SetAttr extends AbstractLogiclet{
        protected String pid = "$md-reattr";
        protected String $key;
        protected String $value;
        protected String $dft;
        protected boolean raw = false;

        public SetAttr(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            pid = PropertiesConstants.getString(p,"pid",pid);
            $key = PropertiesConstants.getRaw(p,"key","");
            $value = PropertiesConstants.getRaw(p,"value","");
            $dft = PropertiesConstants.getRaw(p,"dft","");
            raw = PropertiesConstants.getBoolean(p,"raw",raw);
        }

        @Override
        protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
            Map<String,String> attrMap = ctx.getObject(pid);
            if (attrMap != null){
                String key = PropertiesConstants.transform(ctx,$key,"");
                if (StringUtils.isNotEmpty(key)){
                    String value = raw?PropertiesConstants.getRaw(ctx,$value,""):
                            PropertiesConstants.transform(ctx,$value,"");
                    if (StringUtils.isEmpty(value)){
                        value = PropertiesConstants.transform(ctx,$dft,"");
                    }
                    if (StringUtils.isNotEmpty(value)){
                        attrMap.put(key,value);
                    }
                }
            }else{
                log(String.format("[%s]parent context id null:%s",this.getXmlTag(),pid));
            }
        }
    }
}
